<template>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
  >
  <comment-item  @click-replay="$emit('click-replay',$event)" :comment="item" v-for="item in list" :key="item.com_id+''" :title="item.content" />
  </van-list>
</template>

<script>
import { getCommentList } from '@/api/article.js'
import CommentItem from '@/components/CommentItem'
export default {
  components: { CommentItem },
  props: {
    type: {
      type: String,
      default: 'a'
    },
    source: {
      type: [String, Number, Object],
      required: true
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },

  created () {},

  methods: {
    async onLoad () {
      // 异步更新数据
      // 1.获取数据
      try {
        const { results, last_id: lastId, total_count: total } = await getCommentList({
          type: this.type,
          source: this.source,
          limit: this.limit,
          offset: this.offset
        })
        // 2.保存数据
        // 3.this.list.push(this.list.length + 1)
        this.list.push(...results)
        // 加载状态结束
        // this.loading = false
        this.loading = false
        this.$emit('on-success', total)
        // console.log(total)
        // 数据全部加载完成
        if (!results.length) {
          // this.finished = true
          this.finished = true
        } else {
          this.offset = lastId
        }
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
